﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Web;

namespace TheCup
{
    public class TheCupMailNotifier
    {
        static SmtpClient smtp = new SmtpClient("smtp.gmail.com") { EnableSsl = true, Credentials = new NetworkCredential("thecup.noreply@gmail.com", "TheCup2010") };

        private MailAddress TheCupMailAddress = new MailAddress("TheCup <thecup.noreply@gmail.com>");
        private const string TheCupMailFooter = "<p>Se mere på <a href='http://www.thecup.dk'>The Cup</a></p>";
        private Encoding UNICODE = Encoding.UTF8;
        private MailMessage msg = new MailMessage();

        public TheCupMailNotifier(string Subject, string Body)
        {
            msg.IsBodyHtml = true;
            msg.From = TheCupMailAddress;
            msg.SubjectEncoding = UNICODE;
            msg.BodyEncoding = UNICODE;
            msg.Subject = Subject;
            msg.Body = Body;
        }

        public void AddRecipient(Player to)
        {
            try
            {
                msg.To.Add(new MailAddress(to.Email, to.DisplayName, UNICODE));
            }
            catch (ArgumentException)
            {
                throw;
            }
            catch (FormatException )
            {
                throw;
            }
        }

        public void Send()
        {
            try
            {
                if (!System.Web.HttpContext.Current.IsDebuggingEnabled)
                {
                    if (msg.To.Count > 0)
                    {
                        smtp.Send(this.msg);
                    }
                }
            }
            catch (SmtpFailedRecipientsException)
            {
                throw;
            }
            catch (SmtpException)
            {
                throw;
            }
            finally
            {
                msg.To.Clear();
            }
        }
    }
}